<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="110px" class="demo-ruleForm" size="mini">

      <el-form-item label="学校名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入学校名称" clearable></el-input>
      </el-form-item>

      <el-form-item label="学校代码" prop="deptCode">
        <el-input v-model="ruleForm.deptCode" placeholder="请输入学校代码" clearable></el-input>
      </el-form-item>

      <el-form-item label="教育程度" prop="rank">
        <el-select filterable v-model="ruleForm.rank" placeholder="请选择教育程度" clearable>
          <el-option label="小学" value="小学"></el-option>
          <el-option label="初中" value="初中"></el-option>
          <el-option label="高中" value="高中"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="教育性质" prop="tag">
        <el-select filterable v-model="ruleForm.tag" placeholder="请选择教育性质" clearable>
          <el-option label="公立" value="公立"></el-option>
          <el-option label="私立" value="私立"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="resetForm('ruleForm')">{{$t("action.reset")}}</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t("action.save")}}</el-button>

      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import Cookies from "js-cookie"
  import {format} from "@/utils/datetime"

  export default {
    name: "edit",
    props: {
      msg: ''
    },
    data() {
      return {
        url: this.$Config.url,
        id: '',//修改对应的id
        ruleForm: {
          name: '',//姓名
          deptCode: '',
          rank: '',
          tag: ''
        },
        rules: {
          name: [
            {required: true, message: '请输入学校名称', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
          ],
          deptCode: [
            {required: true, message: '请输入学校代码', trigger: 'blur'}
          ],
          rank: [
            {required: true, message: '请选择教育程度', trigger: 'change'}
          ],
          tag: [
            {required: true, message: '请选择教育性质', trigger: 'change'}
          ],

        },
        formData: {}
      };
    },
    methods: {
      resetData(id = 1) {
        this.$refs["ruleForm"].resetFields();//先清空
        const that = this;
        if (id) {
          this.id = id;
        }
        this.$axios.get(that.url + '/admin/dept/get/{id}', {
          params: {
            id,
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
          .then(function (res) {
            if (res.status == 200) {
              let data = res.data.data;
              that.ruleForm = data;
              that.id = data.id;
            } else {
              that.$message({
                message: res.data.msg,
                type: 'warning'
              });
            }

          })
          .catch(function (error) {
            console.log(error);
          });
      },
      submitForm(formName) {
        const that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            that.send();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      send() {
        const that = this;
        this.$axios.post(that.url + '/admin/dept/saveOrUpdate', that.$qs.stringify(that.ruleForm), {
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
          .then(function (res) {
            if (res.status == 200) {
             if(res.data.code==200){     
              if (that.msg == 1) {
                that.$message({
                  message: "添加成功",
                  type: "success"
                });
              } else {
                that.$message({
                  message:"修改成功",
                  type: "success"
                });
              }
              that.$emit("hideEdit", false);
            }else{
              that.$message({
                message: res.data.msg,
                type: "warning"
              });
            }
            } else {
              that.$message({
                message: res.statusText,
                type: 'warning'
              });
            }

          })
          .catch(function (error) {
            console.log(error);
          });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.ruleForm.id = "";
      }
    }

  }
</script>

<style scoped lang="scss">
  .el-select, .el-date-editor {
    width: 100%;
  }
</style>
